#include <gui/screen2_screen/screen2View.hpp>
extern "C"
{
#include "user_include.h"
}

screen2View::screen2View()
{
}

void screen2View::setupScreen()
{
    screen2ViewBase::setupScreen();
}

void screen2View::tearDownScreen()
{
    screen2ViewBase::tearDownScreen();
}
void screen2View::handleTickEvent(void)
{
    static uint8_t tick = 0;
    static uint8_t fliker = 0;
    uint8_t i = 0;
    struct tm t;
    tick++;
    if (tick % 6 == 0)
    {
        driver_rtc_struct.read(&t);
        digitalClock1.setTime24Hour(t.tm_hour, t.tm_min, t.tm_sec);
        if (driver_button_struct.key_value == BUTTON1_PRESS_FLAG)
        {
            driver_button_struct.key_value = 0;
            application().gotoScreen3ScreenSlideTransitionEast();
        }
        else if (driver_button_struct.key_value == BUTTON1_LONG_PRESS_FLAG)
        {
            driver_button_struct.key_value = 0;
            application().gotoScreen7ScreenSlideTransitionEast();
        }
        else if (driver_button_struct.key_value == BUTTON2_PRESS_FLAG)
        {
            driver_button_struct.key_value = 0;
            application().gotoscreen2ScreenSlideTransitionEast();
        }
        else if (driver_button_struct.key_value == BUTTON2_LONG_PRESS_FLAG)
        {
            driver_button_struct.key_value = 0;
            application().gotoScreen5ScreenSlideTransitionEast();
        }
        else if (driver_button_struct.key_value == BUTTON3_PRESS_FLAG)
        {
            driver_button_struct.key_value = 0;
            application().gotoScreen4ScreenSlideTransitionEast();
        }

        fliker++;
        if (fliker == 3)
        {
            Unicode::snprintf(textAreaH2sBuffer, 10, "%d", driver_adc_struct.table[0]);
            Unicode::snprintf(textAreaLelBuffer, 10, "%d", driver_adc_struct.table[1]);
            Unicode::snprintf(textAreaO2Buffer, 10, "%d", driver_adc_struct.table[2]);
            Unicode::snprintf(textAreaCoBuffer, 10, "%d", driver_adc_struct.table[3]);
        }
        else if (fliker >= 6)
        {
            fliker = 0;
            if (driver_adc_struct.table[0] > 2048)
            {
                Unicode::snprintf(textAreaH2sBuffer, 10, "%s", L" ");
            }
            if (driver_adc_struct.table[1] > 2048)
            {
                Unicode::snprintf(textAreaLelBuffer, 10, "%s", L" ");
            }
            if (driver_adc_struct.table[2] > 2048)
            {
                Unicode::snprintf(textAreaO2Buffer, 10, "%s", L" ");
            }
            if (driver_adc_struct.table[3] > 2048)
            {
                Unicode::snprintf(textAreaCoBuffer, 10, "%s", L" ");
            }
        }
        box1.invalidate();
        textAreaH2s.resizeToCurrentText();
        textAreaLel.resizeToCurrentText();
        textAreaO2.resizeToCurrentText();
        textAreaCo.resizeToCurrentText();
        textAreaH2s.invalidate();
        textAreaLel.invalidate();
        textAreaO2.invalidate();
        textAreaCo.invalidate();
    }
}

void screen2View::model_view(uint8_t flag, ...)
{
    int32_t para[10];
    va_list argptr;
    va_start(argptr, flag);

    if (flag == 1)
    {
        para[0] = va_arg(argptr, int32_t);
        if (para[0] == 1)
        {
            application().gotoScreen3ScreenSlideTransitionEast();
        }
        else if (para[0] == 2)
        {
            application().gotoscreen2ScreenSlideTransitionEast();
        }
        else if (para[0] == 3)
        {
            application().gotoScreen4ScreenSlideTransitionEast();
        }
    }
    else if (flag == 2)
    {
        para[0] = va_arg(argptr, int32_t);
        para[1] = va_arg(argptr, int32_t);
        para[2] = va_arg(argptr, int32_t);
        para[3] = va_arg(argptr, int32_t);

        Unicode::snprintf(textAreaH2sBuffer, 10, "%d", para[0]);
        Unicode::snprintf(textAreaLelBuffer, 10, "%d", para[1]);
        Unicode::snprintf(textAreaO2Buffer, 10, "%d", para[2]);
        Unicode::snprintf(textAreaCoBuffer, 10, "%d", para[3]);
        box1.invalidate();
        textAreaH2s.resizeToCurrentText();
        textAreaLel.resizeToCurrentText();
        textAreaO2.resizeToCurrentText();
        textAreaCo.resizeToCurrentText();
        textAreaH2s.invalidate();
        textAreaLel.invalidate();
        textAreaO2.invalidate();
        textAreaCo.invalidate();
    }
    va_end(argptr);
}